package com.biqee.model.common;

import java.util.HashMap;
import java.util.Map;

import com.biqee.util.StringUtil;


public class PageInfoObject {
    public static final int MAX_LIMIT = 1000;
    //每页显示记录数
    private int limit = 40;
    
    // 当前记录下标，从0开始
    private int start=0;
    
    // 排序方向
    private String dir="asc";

    // 排序字段
    private String sort;
    
    private int count ;
    
    private Map<Object,Object> params;
    
    
    
//    private boolean isStringTypeOrderBy = true;
  
 
	public PageInfoObject(){
        
    }
    /**
     * 直接根据单页数据数,总记录数,排序条件构造一个翻页类
     * 构造函数
     */
    public PageInfoObject(int limit){
        this.limit = limit;
    }
    /**
     * 下一页
     */
    public void turnNextPage(){
      int end = getEnd();
      if(end<count){
          this.start+=limit;
      }else{
          this.start=(count-limit>0?count-limit:0);
      }
    }
    
    /**
     * 上一页
     */
    public void turnPrePage(){
      if(this.start>=this.limit){
          this.start= this.start-this.limit;
      }else{
          this.start =0 ;
      }     
    }

    public int getLimit() {
        return limit;
    }

   

    public String getDir() {
        return dir;
    }

    /**
     * 分页起始位置，从0开始
     */ 
    public int getStart() {
        return start;
    }
    /**
     * 分页结束位置
     */
    public int getEnd() {
        return getStart() + getLimit() - 1;
    }
    
    /**
     * 
     * 总页数
     * @author binda.mabd
     * @date 2009-4-14
     * @return
     */
    public int getPageCount(){
        if(getCount()%getLimit()==0){
            return getCount()/getLimit()==0?1:getCount()/getLimit();
        }else{
            return getCount()/getLimit()+1;
        }
    }
    
    /**
     * 
     * 得到当前页数
     * @author binda.mabd
     * @date 2009-4-14
     * @return
     */
    public int getCurrentPageNo(){
        
      return getStart()/getLimit()+1;
    
    }
    
    
    public void setPageNO(int pageNO){
    	if(pageNO<1){
    		pageNO =1;
    	}
    	this.start = (pageNO-1)*this.limit;
    }
    

    public void setDir(String dir) {
        this.dir = dir;
    }

   

    public void setLimit(int limit) {
    	//安全考虑最大限制
    	if(limit>MAX_LIMIT){
    	  this.limit = MAX_LIMIT;
    	}else{
    	  this.limit = limit;
    	}
    }
    
    public void addParam(String key,Object value){
    	if(this.params==null){
    		this.params = new HashMap<Object,Object>();
    	}
    	this.params.put(key, value);
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = StringUtil.property2Field(sort);
	}
    
    public boolean  isFirstPage(){
    	return this.getCurrentPageNo()==1; 
    }
    
    public boolean isLastPage(){
    	return this.getCurrentPageNo()==this.getPageCount();
    }
    
    public Map<Object, Object> getParams() {
		return params;
	}
	public void setParams(Map<Object, Object> params) {
		this.params = params;
	}
}
